// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:22:08 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   SelectorDeProductos2.java

package com.luxsoft.sw3.impap.ui.selectores;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.matchers.CompositeMatcherEditor;
import ca.odell.glazedlists.swing.EventSelectionModel;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.luxsoft.siipap.model.core.Producto;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.service.core.ProductoManager;
import com.luxsoft.siipap.swing.actions.DispatchingAction;
import com.luxsoft.siipap.swing.controls.SXAbstractDialog;
import com.luxsoft.siipap.swing.utils.ComponentUtils;
import com.luxsoft.siipap.swing.utils.SWExtUIManager;
import com.luxsoft.siipap.swing.utils.TaskUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import org.apache.commons.collections.ListUtils;
import org.jdesktop.swingx.JXTable;

public class SelectorDeProductos2 extends SXAbstractDialog
{

    public SelectorDeProductos2()
    {
        super("Cat\341logo de productos");
        lineaField = new JTextField(10);
        marcaField = new JTextField(10);
        claseField = new JTextField(10);
        productoField = new JTextField(20);
        carasField = new JTextField(10);
        acabadoField = new JTextField(10);
        presentacionField = new JTextField(10);
        anchoField = new JTextField(10);
        largoField = new JTextField(10);
        calibreField = new JTextField(10);
        kilosField = new JTextField(10);
        gramosField = new JTextField(10);
        selectionType = 2;
    }

    protected JComponent buildContent()
    {
        initGlazedLists();
        JPanel content = new JPanel(new BorderLayout(1, 5));
        content.add(buildTopPanel(), "North");
        content.add(buildGridPanel(), "Center");
        content.setPreferredSize(new Dimension(950, 500));
        return content;
    }

    protected JPanel buildTopPanel()
    {
        FormLayout layout = new FormLayout(" p,2dlu,p,\t1dlu, p,2dlu,p, 1dlu, p,2dlu,p, 1dlu, p,2dlu,p, 1dlu, p,2dlu,p", "");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.append("Producto", productoField);
        builder.append("L\355nea", lineaField);
        builder.nextLine();
        builder.append("Caras", carasField);
        builder.append("Acabado", acabadoField);
        builder.append("Presentaci\363n", presentacionField);
        builder.nextLine();
        builder.append("Clase", claseField);
        builder.append("Marca", marcaField);
        builder.nextLine();
        builder.append("Ancho", anchoField);
        builder.append("Largo", largoField);
        builder.append("Calibre", calibreField);
        builder.append("Kilos", kilosField);
        builder.append("Gramos", gramosField);
        ComponentUtils.decorateSpecialFocusTraversal(builder.getPanel());
        return builder.getPanel();
    }

    protected void initGlazedLists()
    {
        source = new BasicEventList();
        EventList editors = new BasicEventList();
        editors.add(new TextComponentMatcherEditor(productoField, GlazedLists.textFilterator(new String[] {
            "clave", "descripcion"
        })));
        editors.add(new TextComponentMatcherEditor(lineaField, GlazedLists.textFilterator(new String[] {
            "linea.nombre"
        })));
        editors.add(new TextComponentMatcherEditor(carasField, GlazedLists.textFilterator(new String[] {
            "caras"
        })));
        editors.add(new TextComponentMatcherEditor(acabadoField, GlazedLists.textFilterator(new String[] {
            "acabado"
        })));
        editors.add(new TextComponentMatcherEditor(presentacionField, GlazedLists.textFilterator(new String[] {
            "presentacion"
        })));
        editors.add(new TextComponentMatcherEditor(marcaField, GlazedLists.textFilterator(new String[] {
            "marca.nombre"
        })));
        editors.add(new TextComponentMatcherEditor(claseField, GlazedLists.textFilterator(new String[] {
            "clase.nombre"
        })));
        editors.add(new TextComponentMatcherEditor(anchoField, GlazedLists.textFilterator(new String[] {
            "ancho"
        })));
        editors.add(new TextComponentMatcherEditor(largoField, GlazedLists.textFilterator(new String[] {
            "largo"
        })));
        editors.add(new TextComponentMatcherEditor(calibreField, GlazedLists.textFilterator(new String[] {
            "calibre"
        })));
        editors.add(new TextComponentMatcherEditor(kilosField, GlazedLists.textFilterator(new String[] {
            "kilos"
        })));
        editors.add(new TextComponentMatcherEditor(gramosField, GlazedLists.textFilterator(new String[] {
            "gramos"
        })));
        CompositeMatcherEditor matcher = new CompositeMatcherEditor(editors);
        source = new FilterList(source, matcher);
        sortedSource = new SortedList(source, null);
    }

    protected JComponent buildGridPanel()
    {
        initGlazedLists();
        grid = ComponentUtils.getStandardTable();
        String props[] = {
            "clave", "descripcion", "nacional", "linea.nombre", "caras", "acabado", "presentacion", "clase.nombre", "marca.nombre", "ancho", 
            "largo", "calibre", "kilos", "gramos"
        };
        String names[] = {
            "clave", "Desc", "Nac", "L\355nea", "Caras", "Acabado", "Presentacion", "Clase", "Marca", "Ancho", 
            "Largo", "Calibre", "Kilos", "Gramos"
        };
        ca.odell.glazedlists.gui.TableFormat tf = GlazedLists.tableFormat(Producto.class, props, names);
        EventTableModel tm = new EventTableModel(sortedSource, tf);
        grid.setModel(tm);
        selectionModel = new EventSelectionModel(sortedSource);
        selectionModel.setSelectionMode(getSelectionType());
        grid.setSelectionModel(selectionModel);
        ComponentUtils.addEnterAction(grid, new DispatchingAction(this, "select"));
        TableComparatorChooser.install(grid, sortedSource, TableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
        grid.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e)
            {
                if(e.getClickCount() == 2)
                    select();
            }

           
        }
);
        grid.setColumnControlVisible(false);
        return ComponentUtils.createTablePanel(grid);
    }

    public void select()
    {
        java.util.List prods = new ArrayList();
        prods.addAll((Collection)selectionModel.getSelected());
        doClose();
        onSelection((java.util.List)selectionModel.getSelected());
    }

    public void onSelection(java.util.List list)
    {
    }

    public int getSelectionType()
    {
        return selectionType;
    }

    public void setSelectionType(int selectionType)
    {
        this.selectionType = selectionType;
    }

    protected java.util.List findData()
    {
        return ServiceLocator2.getProductoManager().buscarProductosActivos();
    }

    public void load()
    {
        SwingWorker worker = new SwingWorker() {

            protected java.util.List doInBackground() throws Exception
            {
                return findData();
            }

            protected void done()
            {
                source.clear();
                try
                {
                    source.addAll((Collection)get());
                    grid.packAll();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
;
        TaskUtils.executeSwingWorker(worker);
    }

    protected void onWindowOpened()
    {
        load();
    }

    public static java.util.List find()
    {
        SelectorDeProductos2 finder = new SelectorDeProductos2();
        finder.open();
        if(!finder.hasBeenCanceled())
            return (java.util.List)finder.selectionModel.getSelected();
        else
            return ListUtils.EMPTY_LIST;
    }

    public static Producto seleccionar()
    {
        SelectorDeProductos2 finder = new SelectorDeProductos2();
        finder.setSelectionType(1);
        finder.open();
        if(!finder.hasBeenCanceled())
            return (Producto)finder.selectionModel.getSelected().get(0);
        else
            return null;
    }

    public static Producto seleccionar(java.util.List productos)
    {
        SelectorDeProductos2 finder = new SelectorDeProductos2();
        finder.setSelectionType(1);
        finder.open();
        if(!finder.hasBeenCanceled())
            return (Producto)finder.selectionModel.getSelected().get(0);
        else
            return null;
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                SWExtUIManager.setup();
                java.util.List selected = SelectorDeProductos2.find();
                System.out.println(selected);
                System.exit(0);
            }

        }
);
    }

    protected JTextField lineaField;
    protected JTextField marcaField;
    protected JTextField claseField;
    protected JTextField productoField;
    protected JTextField carasField;
    protected JTextField acabadoField;
    protected JTextField presentacionField;
    protected JTextField anchoField;
    protected JTextField largoField;
    protected JTextField calibreField;
    protected JTextField kilosField;
    protected JTextField gramosField;
    protected EventList source;
    protected SortedList sortedSource;
    protected JXTable grid;
    protected EventSelectionModel selectionModel;
    private int selectionType;
}
